package com.yylending.las.main.message;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IEvent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by HenryZhang on 2016/11/16.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class MessagePresenter extends Presenter<MessageModel, MessagePresenter.View> {
	private final static String TAG = "MessagePresenter";
	private static final int REQUEST_CODE_REFRESH = 1;
	private static final int REQUEST_CODE_LOAD_MORE = 2;

	public interface View extends IView {
		void onPageListSuccess(boolean isRefresh, List<MessageModel.Message> list);
		void onPageListFailed(String tips);
	}

	public static class Generator implements PresenterFactory<MessagePresenter> {
		@Override
		public MessagePresenter create() {
			return new MessagePresenter();
		}
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new MessageModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getPageList(int pageNum, int pageSize) {
		execute(new EmptyEntity(), REQUEST_CODE_REFRESH, false);
//		MessageModel.RequestParams params = new MessageModel.RequestParams(pageNum, pageSize);
//		if (pageNum > 1) {
//			execute(params, REQUEST_CODE_LOAD_MORE, false);
//		} else {
//			execute(params, REQUEST_CODE_REFRESH, false);
//		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void pageListResult(MessageModel.MsgListResult result) {
		if (BaseResultEntity.isResponseSuccess(result) && null != result.getData()) {
			MessageModel.MsgResponse data = result.getData();
			mView.onPageListSuccess(REQUEST_CODE_REFRESH == result.requestCode, data.rows);
		} else {
			mView.onPageListFailed(result.getRetMsg());
		}
	}
}
